﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using cludg.Infrastructure.DI;

namespace nsadv.Statistic.Web.ControllerFactory
{
	public class InjectedControllerFactory : DefaultControllerFactory
	{
		protected readonly IDIContainer Container;

		public InjectedControllerFactory(IDIContainer container)
		{
			Container = container;
		}

		protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
		{
			if (controllerType == null)
				return null;

			if (!typeof(IController).IsAssignableFrom(controllerType))
				throw new ArgumentException(string.Format(
					"Type requested is not a controller: {0}",
					controllerType.Name),
					"controllerType");

			var controller = Container.Resolve(controllerType, null) as IController;

			return controller;
		}
	}
}
